import React, { Component } from 'react';
import RouterContext from '../RouterContext';
import LifeCycle from './LifeCycle';

export default class Redirect extends Component {
  render() {
    return (
      <RouterContext.Consumer>
        {(context) => {
          const { history } = context;
          const { to, push = false } = this.props;
          return (
            <LifeCycle
              onMount={() => {
                // 跳转需要在子组件 准备挂载 的时候在跳转
                push ? history.push(to) : history.replace(to);
              }}
            />
          );
        }}
      </RouterContext.Consumer>
    );
  }
}
